package site.jlopen.controller;

import java.io.File;
import java.util.Date;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.annotation.Resource;
import site.jlopen.assets.oss.AwsOssConfig;
import site.jlopen.domain.datasource.event.KusciaDatasourceOssEvent;
import site.jlopen.entity.BaseResponse;
import site.jlopen.entity.file.FileUploadRes;
import site.jlopen.service.intf.FileUploadService;
import site.jlopen.service.intf.datasource.DataSourceService;
import site.jlopen.service.intf.oss.OssService;
import site.jlopen.utils.id.IdUtils;
import site.jlopen.utils.id.Seq;

@RestController
@RequestMapping("/file")
public class UploadController {
	private final Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired private OssService ossService;
	@Autowired private DataSourceService dataSourceService;
	@Resource(name="FileUploadService")
	private FileUploadService uploadService;
	
	@PostMapping("/uploadOss")
	public BaseResponse<FileUploadRes> uploadOss(@RequestParam("file") MultipartFile file,
			@RequestParam("datasourceId") Long datasourceId,
			@RequestParam(value="fileName", required = false) String fileName,
			@RequestParam("fileDir") String fileDir) {
		logger.info("file:{}", file);
		if(null == file) {
			return new BaseResponse<FileUploadRes>().error("请上传文件");
		}
		KusciaDatasourceOssEvent ossInfo = dataSourceService.getDatasourceOss(datasourceId);
		AwsOssConfig ossConfig = AwsOssConfig.builder().endpoint(ossInfo.getOssEndpoint()).accessKeyId(ossInfo.getOssAccessKeyId())
				.secretAccessKey(ossInfo.getOssAccessKeySecret()).build();
		return ossService.upload(file, ossConfig, ossInfo.getOssBucket(), ossInfo.getOssPrefix(), fileDir, fileName);
	}

	@PostMapping("/upload")
	public BaseResponse<FileUploadRes> pload(MultipartFile file) {
		try {
			String fileName = String.format("%s_%s", Seq.getId(Seq.uploadSeqType), file.getOriginalFilename());
			FileUploadRes uploadRes = uploadService.upload(file, DateFormatUtils.format(new Date(), "yyyy/MM/dd"), fileName);
			if(uploadRes.getStatus() != 0) {
				return new BaseResponse<FileUploadRes>().error("文件上传失败");
			}
			return new BaseResponse<FileUploadRes>().success("上传成功", uploadRes);
		} catch (Exception e) {
			return new BaseResponse<FileUploadRes>().error("上传失败：" + e.getMessage());
		}
	}

	@PostMapping("/uploadCa")
	public BaseResponse<FileUploadRes> uploadCa(MultipartFile file, String code, String name) {
		try {
			FileUploadRes uploadRes = uploadService.uploadCa(file, code + "/" + String.valueOf(IdUtils.SnowFlakeId()), name);
			if(uploadRes.getStatus() != 0) {
				return new BaseResponse<FileUploadRes>().error("文件上传失败");
			}
			return new BaseResponse<FileUploadRes>().success("上传成功", uploadRes);
		} catch (Exception e) {
			return new BaseResponse<FileUploadRes>().error("上传失败：" + e.getMessage());
		}
	}
}